package cn.wolfcode.controller;


import cn.wolfcode.domain.CustomerTraceHistory;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.query.JsonResult;
import cn.wolfcode.query.KeywordQueryObject;
import cn.wolfcode.service.ICustomerTraceHistoryService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 客户历史之跟进历史
 */
@Api(value = "客户跟进历史",description = "客户跟进历史资源控制器")
@Controller
@RequestMapping("/customerTraceHistory")
public class CustomerTraceHistoryController {
    @Autowired
    private ICustomerTraceHistoryService customerTraceHistoryService;

    // 处理查询所有部门请求
    @RequestMapping("/list")
    @RequiresPermissions(value = {"customerTraceHistory:list","客户跟进历史列表"},logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") KeywordQueryObject qo){
        PageInfo<CustomerTraceHistory> pageInfo = customerTraceHistoryService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "customertracehistory/list";
    }
    // 处理删除请求
    @GetMapping("/delete")
    public String delete(Long id){
        if (id != null) {
            customerTraceHistoryService.delete(id);
        }
        return "redirect:/customerTraceHistory/list.do"; // 让浏览器重新发一次请求
    }

    @PostMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customerTraceHistory:saveOrUpdate","客户跟进历史添加/更新"})
    @ResponseBody
    public JsonResult saveOrUpdate(CustomerTraceHistory customerTraceHistory){
        JsonResult js = new JsonResult();
        try {
            if (customerTraceHistory.getId() == null){
                //新增
                //获取当前用户主体
                Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
                customerTraceHistory.setInputUser(employee);
                customerTraceHistory.setInputTime(new Date());
                customerTraceHistoryService.save(customerTraceHistory);
            }else {
                //修改
            }
            js.setFlag(true);
            js.setMsg("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            js.setFlag(false);
            js.setMsg("操作失败");
        }
        return js;
    }

}

